package com.xhu.collegestudentrentalsystem.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhu.collegestudentrentalsystem.module.user.dao.UserDAO;
import com.xhu.collegestudentrentalsystem.module.user.entity.User;
import com.xhu.collegestudentrentalsystem.module.user.service.IUserService;
import com.xhu.collegestudentrentalsystem.util.ResultData;
import com.xhu.collegestudentrentalsystem.util.constant.EntityProperty;
import com.xhu.collegestudentrentalsystem.util.constant.PointMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhu huilin
 * @date 2023/2/26 11:45
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDAO userDAO;


    /**
     * 用户注册
     * @param user 用户信息，包含用户昵称、电子邮箱、手机号码、密码、角色信息、时间
     * @return
     */
    @Override
    public ResultData register(User user) {
        // 插入数据
        int id = userDAO.insert(user);
        if(id > 0){
            return new ResultData(ResultData.OPERATION_SUCCESS, PointMsg.register.REGISTER_SUCCESS);
        }
        return new ResultData(ResultData.OPERATION_FAIL, PointMsg.register.REGISTER_FAIL);
    }

    /**
     * 查询数据库中是否存在当前邮箱信息，true为存在，false不存在
     * @param email 邮箱
     * @return
     */
    @Override
    public Boolean isEmailExist(String email) {
        User user = userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.user.USER_EMAIL, email));
        if(user == null){
            return false;
        }
        return true;
    }

    /**
     * 查询数据库中是否存在当前用户账号信息，true为存在，false不存在
     * @param username
     * @return
     */
    @Override
    public Boolean isUsernameExist(String username) {
        User user = userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.user.USERNAME, username));
        if(user == null){
            return false;
        }
        return true;
    }

    /**
     * 根据用户名查询用户在数据库中的信息
     * @param username  用户名
     * @return
     */
    @Override
    public ResultData verifyUser(String username, String password, String role) {
        User user = userDAO.selectOne(
                new QueryWrapper<User>()
                        .eq(EntityProperty.user.USERNAME, username)
                        .eq(EntityProperty.user.USER_ROLE, role));
        if(user == null){
            return new ResultData(ResultData.OPERATION_FAIL, PointMsg.login.UN_REGISTER);
        }else if(!user.getPassword().equals(password)){
            return new ResultData(ResultData.LOGIN_FAIL, PointMsg.login.ERROR_PASSWORD);
        }
        return new ResultData(ResultData.LOGIN_SUCCESS, PointMsg.login.LOGIN_SUCCESS, user);
    }

    /**
     * 根据用户名查询用户数据
     * @param username 用户名（昵称）
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        return userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.user.USERNAME, username));
    }

    /**
     * 根据邮箱查询用户信息
     */
    @Override
    public User findUserByEmail(String email) {
        return userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.user.USER_EMAIL, email));
    }

    /**
     * 根据用户id查询用户信息
     */
    @Override
    public User findUserById(Long userId) {
        User user = userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.ID, userId));
        return user;
    }

    /**
     * 修改用户信息，并返回修改后的用户实体信息
     *      如果头像为空，就不修改头像字段
     *      如果头像有值，就修改头像字段
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public User updateUserInfo(User user) {
        if(user.getUserHead() == null){
            userDAO.updateUserInfo(user);
        }else {
            userDAO.updateUserInfoAndHead(user);
        }
        // 查询信息
        return userDAO.selectOne(new QueryWrapper<User>().eq(EntityProperty.ID, user.getId()));
    }

    /**
     * 根据用户ID修改用户密码
     */
    @Override
    public Integer updatePassword(Long userId, String password) {
        return userDAO.updateUserPass(userId, password);
    }

    /**
     * 根据邮箱修改用户密码
     */
    @Override
    public Integer updatePasswordByEmail(String email, String password) {
        return userDAO.updateUserPassByEmail(email, password);
    }
}
